package com.htu.controller;

import com.htu.constant.CResultType;
import com.htu.domain.Cart;
import com.htu.domain.Order;
import com.htu.domain.Result;
import com.htu.domain.User;
import com.htu.model.ICartModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class CartController
{
    @Autowired
    private ICartModel model;
    @PostMapping("/queryCartById")
    public Result<List<Cart>> queryCartById(int id)
    {
        List<Cart> cartMsg = model.queryCartById(id);
        int resultType = CResultType.FAIL;
        String msg = "fail";
        if (cartMsg.size() != 0)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result< List<Cart>>(resultType, msg, cartMsg);
    }
    @PostMapping("/updateCart")
    public Result<Boolean> doUpdateUser(int id, String name, @RequestParam(value = "count", required = false) int count)
    {
        boolean updateState = model.doUpdateCart( id, name, count);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (updateState)
        {
            msg = "success";
            resultType = CResultType.SUCCESS;
        }
        return new Result<Boolean>(resultType, msg, updateState);
    }
    @PostMapping("/cart/delete")
    public Result<Boolean> doCartDelete(int id,String name)
    {
        boolean cart = model.doCartDelete(id,name);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (cart)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<Boolean>(resultType, msg, cart);
    }
    @PostMapping("/cart/insert")
    public Result<Boolean> doCartInsert(Cart cart)
    {
        boolean cartInsertState = model.doCartInsert( cart);
        String msg = "fail";
        int resultType = CResultType.FAIL;
        if (cartInsertState)
        {
            resultType = CResultType.SUCCESS;
            msg = "success";
        }
        return new Result<Boolean>(resultType, msg, cartInsertState);
    }

}
